// router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import Layout from '../components/MainLayout.vue'
import Login from '../views/login/LoginView.vue'
import Register from '../views/login/RegisterView.vue'
import Forget from '../views/login/ForgetView.vue'

const routes = [
    {
        path: '/',
        redirect: '/login' // 重定向到登录页面
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/forget',
        name: 'Forget',
        component: Forget
    },
    {
        path: '/patient',
        component: Layout,
        children:[
            {
                path:'',
                name: 'Home',
                component:() => import('@/views/HomeView.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                }
            },
            {
                path:'personal',
                name: 'Personal',
                component:() => import('@/views/PersonalView.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                }
            },
            {
                path:'reg_appoint',
                name: 'RegisterAppoint',
                component:() => import('@/views/register/RegisterAppoint.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                },
                redirect: '/patient/reg_appoint',  // 添加这行进行重定向
                children:[
                    {
                        path:'',
                        name: 'ChooseClinic',
                        component:() => import('@/views/register/ChooseClinic.vue'),
                        meta:{
                            requireAuth: true, //标记需要验证的路由
                        },
                    },
                    {
                        path:'cho_doctor',
                        name: 'ChooseRegDoctor',
                        component:() => import('@/views/register/ChooseDoctor.vue'),
                        meta:{
                            requireAuth: true, //标记需要验证的路由
                        },
                    },
                    {
                        path:'info_confirm',
                        name: 'RegisterConfirm',
                        component:() => import('@/views/register/RegisterConfirm.vue'),
                        meta:{
                            requireAuth: true, //标记需要验证的路由
                        },
                    }
                ]
            },
            {
                path:'reg_status',
                name: 'RegisterStatus',
                component:() => import('@/views/register/RegisterStatus.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                }
            },
            {
                path:'reg_record',
                name: 'RegisterRecord',
                component:() => import('@/views/register/RegisterRecord.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                }
            },
            {
                path:'reg_record/reg_details',
                name: 'RegisterDetails',
                component:() => import('@/views/register/RegisterDetails.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                }
            },
            {
                path:'exam_appoint',
                name: 'ExamAppoint',
                component:() => import('@/views/exam/ExamAppoint.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                },
                redirect: '/patient/exam_appoint',  // 添加这行进行重定向
                children:[
                    {
                        path:'',
                        name: 'ChooseItem',
                        component:() => import('@/views/exam/ChooseItem.vue'),
                        meta:{
                            requireAuth: true, //标记需要验证的路由
                        },
                    },
                    {
                        path:'cho_doctor',
                        name: 'ChooseExamDoctor',
                        component:() => import('@/views/exam/ChooseDoctor.vue'),
                        meta:{
                            requireAuth: true, //标记需要验证的路由
                        },
                    },
                    {
                        path:'info_confirm',
                        name: 'ExamConfirm',
                        component:() => import('@/views/exam/ExamConfirm.vue'),
                        meta:{
                            requireAuth: true, //标记需要验证的路由
                        },
                    },
                ]
            },
            {
                path:'exam_status',
                name: 'ExamStatus',
                component:() => import('@/views/exam/ExamStatus.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                }
            },
            {
                path:'exam_record',
                name: 'ExamRecord',
                component:() => import('@/views/exam/ExamRecord.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                }
            },
            {
                path:'exam_record/exam_details',
                name: 'ExamDetails',
                component:() => import('@/views/exam/ExamDetails.vue'),
                meta:{
                    requireAuth: true, //标记需要验证的路由
                }
            },

        ]
    },
    // 定义其他路由
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
});

//全局前置守卫路由
router.beforeEach((to,from,next) =>{
    if(to.matched.some(record => record.meta.requireAuth)){
        // 该模块是需要验证的
        // 这里应该是检查用户是否登录的逻辑
        // 假设有一个方法 checkIfUserIsLoggedIn() 来检查登录状态
        if(!checkIfUserIsLoggedIn()){
            // 未登录——重定向回登录页面
            next({ name: 'Login' });
        }else{
            // 已登录——直接放行到下个页面
            next();
        }
    }else{
        // 不需要验证的话就直接放行
        next();
    }
});

export default router;

function checkIfUserIsLoggedIn(){
    return !!sessionStorage.getItem('authorization');
}
